package org.jeecg.modules.flow.callback.takestockplan;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlan;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlanExecute;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlanExecuteMaterial;
import org.jeecg.modules.business.takestockplan.entity.VO.TakeStockPlanExecuteAddVO;
import org.jeecg.modules.business.takestockplan.service.ITakeStockPlanExecuteMaterialService;
import org.jeecg.modules.business.takestockplan.service.ITakeStockPlanExecuteService;
import org.jeecg.modules.business.takestockplan.service.ITakeStockPlanService;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    盘点任务提交
 * 创建人员:    lumingyi
 * 创建时间:    2023-10-04
 */
@Component
public class TakeStockPlanExecuteCommitBean<T> extends BaseCustomBeanRun<T> {

    @Autowired
    private ITakeStockPlanExecuteService takeStockPlanExecuteService;
    @Autowired
    private ITakeStockPlanService takeStockPlanService;
    @Autowired
    private ITakeStockPlanExecuteMaterialService takeStockPlanExecuteMaterialService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(TakeStockPlanExecuteAddVO.class);
        TakeStockPlanExecuteAddVO vo = (TakeStockPlanExecuteAddVO)baseFlowDto.getData();
        List<TakeStockPlanExecuteMaterial> list = vo.getTakeStockPlanExecuteMaterialList();
        if (CollectionUtils.isEmpty(list)) {
            list = takeStockPlanExecuteMaterialService.selectByMainId(vo.getId());
        }
        for (TakeStockPlanExecuteMaterial material : list) {
            if (StringUtils.isBlank(material.getRealCount())) {
                material.setRealCount("0");
                material.setRealCountAssistant("0");
                BigDecimal subtract = material.getStockQuantity().subtract(new BigDecimal("0"));
                if (subtract.compareTo(new BigDecimal("0")) == 0) {
                    material.setResult("4");
                } else {
                    material.setInventoryLoss(NumberUtil.toStr(subtract));
                    material.setResult("2");
                }
            }
        }
        if (StringUtils.isBlank(vo.getCode())) {
            vo.setTakeStockPlanExecuteMaterialList(list);
        }
        // 保存
        if (StringUtils.isBlank(vo.getId())) {
            String id = takeStockPlanExecuteService.saveExecute(vo);
            vo.setId(id);
            baseFlowDto.setInfoId(id);
        } else {
            takeStockPlanExecuteService.updateExecute(vo);
        }
        // 提交
        TakeStockPlanExecute byId = takeStockPlanExecuteService.getById(vo.getId());
        byId.setDocumentStatus("2");
        takeStockPlanExecuteService.updateById(byId);
        takeStockPlanService.update(new LambdaUpdateWrapper<TakeStockPlan>()
                .eq(TakeStockPlan::getId, byId.getWmsTakeStockPlanId())
                .set(TakeStockPlan::getDocumentStatus, "4"));
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult("提交成功");
    }
}
